// 公司列表接口
import { companiesList } from '@/api/company.js'

const state = {
  // 公司列表
  list: [],
  // 是否清空列表
  isReset: false,
  // 公司列表总条数
  total: 0,
  form: {
    start: 0,
    limit: 5,
    longitude: 116.403963,
    latitude: 39.915119,
    q: '',
    distance: null,
    score: null,
    scoreRange: null
  }
}
const mutations = {
  // 初始化form
  initForm (state) {
    state.form = { start: 0, limit: 5 }
  },
  initList (state) {
    state.list = []
  },
  // 修改起始索引
  setStart (state, value) {
    state.form.start = value
  },
  // 修改检索个数
  setLimit (state, value) {
    state.form.limit = value
  },
  // 修改关键字
  setQ (state, value) {
    state.form.q = value
  },
  // 修改距离升降序
  setDistance (state, value) {
    if (!value) {
      return
    }
    state.form.distance = value
  },
  // 修改评分升降序
  setScore (state, value) {
    state.form.score = value
  },
  // 修改评分范围
  setScoreRange (state, value) {
    if (!value) {
      return
    }
    state.form.scoreRange = value
  },

  // 修改清空列表参数
  changeReset (state, value) {
    state.isReset = value
  },
  // 赋值公司列表接口
  setCompanyList (state, obj) {
    if (state.isReset) {
      state.list = []
      state.isReset = false
    }
    state.list.push(...obj.list)
    state.total = obj.total
  }
}
const actions = {
  // 调用接口，获取完整公司列表
  async getCompanyList ({ commit }, data) {
    // 调用接口
    const res = await companiesList(data)
    // console.log('res: ', res)
    commit('setCompanyList', {
      list: res.data.list,
      total: res.data.total
    })
  }
}
const getters = {}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
